package solucionessgh.Moneda;


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class MonedaModel extends DataBase{
    
    private int id;//moncod
    private String mondesc;
    private boolean monpred;
    private String monsim;
    private boolean monact;


     

    public MonedaModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO moneda( "+
                                                              " mondesc, monpred, monsim, monact) "+
                                                              "VALUES (?, ?, ?, ?);");
                stmt.setString(1,this.mondesc);
                stmt.setBoolean(2,this.monpred);
                stmt.setString(3,this.monsim);
                stmt.setBoolean(4,this.monact);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE moneda SET mondesc=?, monpred=?, monsim=?, monact=? WHERE moncod = ? ;");
                stmt.setString(1,this.mondesc);
                stmt.setBoolean(2,this.monpred);
                stmt.setString(3,this.monsim);
                stmt.setBoolean(4,this.monact);
                stmt.setInt(5, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from moneda where id=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public ResultSet read(){
        return this.find("select moncod,mondesc,monsim," +
                         "CASE WHEN monact=False THEN 'No' " +
                         "     WHEN monact=True THEN 'Si' " +
                         "END as monact, " +
                         "CASE WHEN monpred=False THEN 'No' " +
                         "    WHEN monpred=True THEN 'Si' " +
                         "END as monpred " +
                         "from moneda;");
    }

    public ResultSet finder(String var){
        return this.find("select moncod,mondesc,monsim,CASE WHEN monact=False THEN 'No' "+
                         "WHEN monact=True THEN 'Si' END as monact, CASE WHEN monpred=False THEN 'No' "+
                         "WHEN monpred=True THEN 'Si' END as monpred from moneda WHERE mondesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT moncod, mondesc, monpred, monsim, monact "+
                         "FROM moneda WHERE moncod = "+var+";");
    }
    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isMonact() {
        return monact;
    }

    public void setMonact(boolean monact) {
        this.monact = monact;
    }
    public String getMondesc() {
        return mondesc;
    }

    public void setMondesc(String mondesc) {
        this.mondesc = mondesc;
    }

    public boolean isMonpred() {
        return monpred;
    }

    public void setMonpred(boolean monpred) {
        this.monpred = monpred;
    }

    public String getMonsim() {
        return monsim;
    }

    public void setMonsim(String monsim) {
        this.monsim = monsim;
    }
       
}
